﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace FierceWar
{
	public class ProgressBar
	{
		private GameResources _resources;
		public Vector2 Position;

		public float Minimum;
		public float Maximum;

		private float _value;
		public float Value
		{
			get
			{
				return _value;
			}
			set
			{
				_value = MathHelper.Clamp(value, Minimum, Maximum);
			}
		}

		public int Width;
		public int Height;

		public int BorderWidth;
		public int BorderHeight;

		private Rectangle _outerRectangle;
		private Rectangle _innerRectangle;
		private Rectangle _valueRectangle;

		public Color BorderColor;
		public Color BackgroundColor;
		public Color ForegroundColor;

		public ProgressBar(GameResources resources, Vector2 position,
			float minimum, float maximum, float value,
			int width, int height, int borderWidth, int borderHeight,
			Color border, Color background, Color foreground)
		{
			_resources = resources;
			Position = position;

			Minimum = minimum;
			Maximum = maximum;
			Value = value;
			
			Width = width;
			Height = height;
			BorderWidth = borderWidth;
			BorderHeight = borderHeight;
			
			BorderColor = border;
			BackgroundColor = background;
			ForegroundColor = foreground;

			_outerRectangle = 
				new Rectangle(
					(int)position.X, (int)position.Y,
					width, height
					);
			_innerRectangle =
				new Rectangle(
					(int)position.X + borderWidth, (int)position.Y + borderHeight,
					width - (2 * borderWidth), height - (2 * borderHeight)
					);
		}

		public void Update()
		{
			float progress = Value / (Maximum - Minimum);
			int currentWidth = (int)Math.Ceiling(
				(double)(progress * _innerRectangle.Width));

			_valueRectangle =
				new Rectangle(
					_innerRectangle.Left, _innerRectangle.Top,
					currentWidth, _innerRectangle.Height
					);
		}

		public void Draw()
		{
			_resources.SpriteBatch.Draw(
				_resources.BlankTexture, _outerRectangle, BorderColor);
			_resources.SpriteBatch.Draw(
				_resources.BlankTexture, _innerRectangle, BackgroundColor);
			_resources.SpriteBatch.Draw(
				_resources.BlankTexture, _valueRectangle, ForegroundColor);
		}
	}
}
